#include "videocapture.h"

VideoCapture::VideoCapture(MyThread *myThread, QWidget *parent)
    :QGLWidget(QGLFormat(QGL::DepthBuffer),parent),m_myThread(myThread),m_image(0)
{
    setFixedSize(640,480);
    setAutoFillBackground(false);
    QObject::connect(m_myThread, SIGNAL(renderedImage(QImage)), this, SLOT(animate(QImage)));
    m_myThread->openCamara();
    m_myThread->start();
}
VideoCapture::~VideoCapture()
{
    if(m_myThread->isRunning())
    {
        m_myThread->stop();
    }
    while (!m_myThread->isFinished()) {


    }
    m_myThread->closeCamara();
    m_myThread->quit();
    delete m_myThread;
    m_myThread=0;
}

void VideoCapture::animate(QImage image)
{
    m_image=image;
    repaint();
}

void VideoCapture::paintEvent(QPaintEvent *event)
{
    QPainter painter;
    painter.begin(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.drawImage(event->rect(),m_image);
    painter.end();
}
